function [codesRet, startSitesRet, endSitesRet] = collectAllCombinedTraits( input )
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here
numCells = size(input, 1);
numSites = size(input, 2);
fprintf('%d cells, %d sites\n', numCells, numSites);
traitIdx = 0;

for i = 1:numCells
    thisCellTraitIdx = 0;
    thisCellEndSites = [];
    firstTransFound = 0;
    for j = 1:numSites
        if (input(i, j) ~= 0)
            [transFrom, transTo] = getTransitionFromCode(input(i, j));
            if ((transFrom > transTo && transTo < 2) || (transFrom < transTo && transTo > 2))
                incr = 1;
                if (transFrom > transTo) incr = -1; end
                n = transFrom;
                while n ~= transTo
                    if ((n > n + incr && n + incr < 2) || (n < n + incr && n + incr > 2))
                        firstTransFound = 1;
                        traitIdx = traitIdx + 1;
                        startSites(traitIdx, 1) = j;
                        codes(traitIdx, 1) = getTransitionCode(n, n + incr);

                        foundEndSite = 0;
                        if (transFrom < transTo)
                            for k = j:numSites
                                [newTransFrom, newTransTo] = getTransitionFromCode(input(i, k));
                                if (newTransTo + newTransFrom ~= 0 && newTransTo <= n)
                                    endSites(traitIdx, 1) = k;
                                    foundEndSite = 1;
                                    break;
                                end
                            end
                        else
                            for k = j:numSites
                                [newTransFrom, newTransTo] = getTransitionFromCode(input(i, k));
                                if (newTransTo + newTransFrom ~= 0 && newTransTo >= n)
                                    endSites(traitIdx, 1) = k;
                                    foundEndSite = 1;
                                    break;
                                end
                            end
                        end
                        
                        if (foundEndSite == 1)
%                             for k = 1:size(chrEndSites, 1)
%                                 if (startSites(traitIdx, 1) <= chrEndSites(k, 1) && endSites(traitIdx, 1) > chrEndSites(k, 1))
%                                     foundEndSite = 0;
%                                 end
%                             end
                        end
                        
                        if (foundEndSite == 0)
                            endSites(traitIdx, 1) = -1;
                        end
                    elseif (firstTransFound == 0)
                            firstTransFound = 1;
                            traitIdx = traitIdx + 1;
                            startSites(traitIdx, 1) = -1;
                            endSites(traitIdx, 1) = j;
                            codes(traitIdx, 1) = getTransitionCode(n+incr, n);
                    end
                    n = n + incr;
                end
            else
                if (firstTransFound == 0)
                    firstTransFound = 1;
                    incr = 1;
                    if (transFrom > transTo) incr = -1; end
                    n = transFrom;
                    while (n ~= transTo)
                        traitIdx = traitIdx + 1;
                        startSites(traitIdx, 1) = -1;
                        endSites(traitIdx, 1) = j;
                        codes(traitIdx, 1) = getTransitionCode(n+incr, n);
                        n = n + incr;
                    end
                end
            end
        end
    end
end

sorted = [codes, startSites, endSites];
sorted = sortrows(sorted, [1, 2, 3]);
condensedIdx = 1;
last = sorted(1, :);
condensed(1, :) = last(1, :);
for i = 2:size(sorted, 1)
    if (sorted(i, 1) == last(1, 1) && sorted(i, 2) == last(1, 2) && sorted(i, 3) == last(1, 3))
        
    else
        condensedIdx = condensedIdx + 1;
        condensed(condensedIdx, :) = sorted(i, :);
        last = sorted(i, :);
    end
end

codesRet(:, 1) = condensed(:, 1);
startSitesRet(:, 1) = condensed(:, 2);
endSitesRet(:, 1) = condensed(:, 3);



end

